package com.lightsh.common;

import com.lightsh.handwritten.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;

/**
 * FIXME 完善!!!
 *
 */
public class BottomBarLayout extends RelativeLayout{

	private View mBottomBarLayout = null;
	private Button mLeftBtn;
	private Button mRightBtn;
	private Animation mDisappearAnimation;
	private Animation mAppearAnimation;
	
	
	public BottomBarLayout(Context context) {
		super(context);
	}
	public BottomBarLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	// FIXME 这里应该是传入按钮个数等参数。。。。
	public void init(View bottomBar){
		mBottomBarLayout = bottomBar;
		mLeftBtn = (Button)mBottomBarLayout.findViewById(R.id.btnLeft);
		mRightBtn = (Button)mBottomBarLayout.findViewById(R.id.btnRight);
		mLeftBtn.setText(R.string.delete);
		mRightBtn.setText(R.string.selectAll);
	}
	
	public boolean isBottomBarVisible(){
		return mBottomBarLayout != null && mBottomBarLayout.getVisibility() == View.VISIBLE;
	}
	
	/**
	 * 设置bottom显示
	 * @param leftText 		左边按钮文字,暂时为"全选"
	 * @param rightText 	右边按钮文字
	 * @param onBtnClickListener 按钮按下事件回调，如果传入null则不改变原值
	 */
	public void setShowBottomBar(String rightText, String leftText, OnClickListener onBtnClickListener){
		if (onBtnClickListener != null) {
			mLeftBtn.setOnClickListener(onBtnClickListener);
			mRightBtn.setOnClickListener(onBtnClickListener);
		}

		mRightBtn.setText(rightText);
		mLeftBtn.setText(leftText);
		if (!isBottomBarVisible()) {
			// 设置状态并显示动画
			mBottomBarLayout.setVisibility(View.VISIBLE);
			if (mAppearAnimation == null) {
				mAppearAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.footer_appear);
			}
			mBottomBarLayout.startAnimation(mAppearAnimation);
		}
	}
	
	/**
	 * 设置bottom显示
	 * 
	 * @param onBtnClickListener 按钮按下事件回调，如果传入null则不改变原值
	 */
	public void setShowBottomBar(boolean bSelectedAll){
		if(bSelectedAll){
			mRightBtn.setText(R.string.selectNone);
		}else{
			mRightBtn.setText(R.string.selectAll);
		}
		if (!isBottomBarVisible()) {
			// 设置状态并显示动画
			mBottomBarLayout.setVisibility(View.VISIBLE);
			if (mAppearAnimation == null) {
				mAppearAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.footer_appear);
			}
			mBottomBarLayout.startAnimation(mAppearAnimation);
		}
	}
	/**
	 * 设置button的listener
	 */
	public void updateBottomBarListener(OnClickListener onBtnClickListener){
		if (onBtnClickListener != null) {
			mLeftBtn.setOnClickListener(onBtnClickListener);
			mRightBtn.setOnClickListener(onBtnClickListener);
		}
	}
	
	public void hideBottomBar(){
		if(isBottomBarVisible()){
			mBottomBarLayout.setVisibility(View.INVISIBLE);
			if(mDisappearAnimation == null){
				mDisappearAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.footer_disappear);
			}
			mBottomBarLayout.startAnimation(mDisappearAnimation);
		}
	}
}
